package module03.Question4;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * 实现学生管理系统中的功能
 */

public class ManageStu {


    private List<Student> studentList;//存储学生信息
    Scanner sc = new Scanner(System.in);//构建扫描器

    public ManageStu(List<Student> studentList) {
        this.studentList = studentList;
    }


    //设置学生信息
    public void setStudentList() {
        System.out.println("请输入新增学生的学号，姓名，年龄：");
        String[] split = sc.next().split(",");
        for (Student stu : studentList) {
            if (split[0].equals(stu.getId())) {
                System.out.println("该学号已存在，新增失败");
                return;
            }
        }
        Student student = new Student(split[0], split[1], Integer.parseInt(split[2]));
        studentList.add(student);
        System.out.println("新增成功");
    }

    //查询学生信息
    public void getStudentList() {
        System.out.println("请输入要查找的学生学号：");
        String id = sc.next();
        for(Student stu1 : studentList) {
            if (id.equals(stu1.getId())){
                System.out.println("该学生的信息是：" + stu1);
                return;
            }
        }
        System.out.println("该学生不存在");
    }

    //删除学生信息
    public void deleteStu() {
        System.out.println("请输入要删除的学生学号：");
        String id = sc.next();
        Iterator<Student> iterator = studentList.iterator();//构建迭代器
        while (iterator.hasNext()) {
            Student next = iterator.next();
            if (id.equals(next.getId())) {
                iterator.remove();
                System.out.println(id + "学生删除成功");
                return;
            }
            System.out.println("该学生不存在");
        }
    }

    //修改学生信息
    public void changeStu() {
        System.out.println("请输入要修改的学生学号：");
        String id = sc.next();
        for (int i = 0; i < studentList.size(); i++) {
            if (id.equals(studentList.get(i).getId())) {
                System.out.println("请输入要修改的姓名，年龄：");
                String name = sc.next();
                int age = sc.nextInt();
                Student student = new Student(id, name, age);
                studentList.set(i, student);
                System.out.println(id + "学生修改后的信息是：" + student);
                return;
            }
        }
        System.out.println("该学生不存在");
    }

    //打印所有学生信息
    public void printAll() {
        System.out.println("所有学生的信息如下：");
        for (Student stu2 : studentList) {
            System.out.println(stu2);
        }
    }
}
